package com.javabasic.algorithm.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see [532. 数组中的 k-diff 数对 - 力扣（LeetCode）](https://leetcode.cn/problems/k-diff-pairs-in-an-array/)
 * @since Created by work on 2022/6/16 23:11
 */
public class KDiffPairsInAnArray {


    public int findPairs(int[] nums, int k) {
        int len = nums.length;
        if (len == 0) {
            return 0;
        }

        Map<Integer, Integer> numMap = new HashMap<>(16);
        Integer num;
        for (int i = 0; i < len; i++) {
            num = numMap.getOrDefault(nums[i], 0);
            num++;
            numMap.put(nums[i], num);
        }

        int result = 0, abs;
        for (Integer key : numMap.keySet()) {
            if (k == 0) {
                if (numMap.get(key) > 1) {
                    result++;
                }
            } else {
                if (numMap.containsKey(key + k)) {
                    result++;
                }
            }
        }

        return result;
    }

}
